package com.jiudengnile.dr.biz.ao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wint.help.biz.result.Result;
import wint.help.biz.result.ResultSupport;
import wint.help.biz.result.StringResultCode;
import wint.help.biz.result.results.CommonResultCodes;
import wint.lang.utils.StringUtil;

import com.jiudengnile.dr.biz.dao.RecordDataDAO;
import com.jiudengnile.dr.biz.domains.RecordDataDO;

public class RecordDataAOImpl implements RecordDataAO {
	
	private Logger log = LoggerFactory.getLogger(getClass());

	private RecordDataDAO recordDataDAO;
	
	private static final long DAY_IN_MS = 1000L * 3600 * 24;
	
	@Override
	public Result queryByDate(String uid, Date start, Date end) {
		Result result = new ResultSupport(false);
		try {
			if (start == null || end == null) {
				return result;
			}
			if (StringUtil.isEmpty(uid)) {
				result.setResultCode(new StringResultCode("uid can not be empty."));
				return result;
			}
			long deltaDays = end.getTime() - start.getTime();
			if (deltaDays < 0) {
				result.setResultCode(new StringResultCode("start date is after end date."));
				return result;
			}
			long day = deltaDays / DAY_IN_MS; 
			if (day > 5) {
				result.setResultCode(new StringResultCode("the date range is to big!"));
				return result;
			}
			
			List<RecordDataDO> data = recordDataDAO.queryLocations(uid, start, end);
			result.getModels().put("data", data);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResultCode(CommonResultCodes.SYSTEM_ERROR);
			log.error("recordData", e);
		}
		return result;
	}

	@Override
	public Result recordData(String uid, String data) {
		Result result = new ResultSupport(false);
		try {
			if (StringUtil.isEmpty(uid) || StringUtil.isEmpty(data)) {
				result.setResultCode(new StringResultCode("uid or data must not be empty!"));
				return result;
			}
			int count = 0;
			String[] parts = data.split("\\r\\n");
			for (String part : parts) {
				
				String stime = StringUtil.getFirstBefore(part, ":");
				Date time = parseTime(stime);
				if (time == null) {
					continue;
				}
				String loc = StringUtil.getFirstAfter(part, ":");
				if (StringUtil.isEmpty(loc)) {
					continue;
				}
				RecordDataDO recordData = new RecordDataDO();
				recordData.setAppType(1);
				recordData.setLocation(loc);
				recordData.setTime(time);
				recordData.setUid(uid);
				recordDataDAO.create(recordData);
				count++;
			}
			result.getModels().put("count", count);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResultCode(CommonResultCodes.SYSTEM_ERROR);
			log.error("recordData", e);
		}
		return result;
	}

	static Date parseTime(String source) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			return sdf.parse(source);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public void setRecordDataDAO(RecordDataDAO recordDataDAO) {
		this.recordDataDAO = recordDataDAO;
	}
	
}
